﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WLMCoreLibrary;
using MiWebSite_Core;

namespace MWS_AddDirectory.MisAnuncios
{
    public partial class Registrarse : System.Web.UI.Page
    {
        public dbConnector ConexionDB;
        public WebSiteMainConfiguration config;
        protected void Page_Load(object sender, EventArgs e)
        {
            config = WebSiteMainConfiguration.GetConfig();
            if (!IsPostBack)
            {
                lblDirectorio.Text = DeCurrentDomain.DomainName;
                lblDirectorio2.Text = DeCurrentDomain.DomainName;
                lblDirectorio3.Text = DeCurrentDomain.DomainName;
            }
        }

        protected void btnRegistrar_Click(object sender, EventArgs e)
        {
            guardarDatosUsuario();
        }

        protected void guardarDatosUsuario()
        {

            ConexionDB = new dbConnector(config.Connections["WebSite"].value);
            Response.ContentType = "text/xml";
            try
            {

                Cliente cliente = new Cliente(ref ConexionDB);
                cliente.email = username.Text;
                cliente.Nombre = username.Text;
                SaltedHash hash = new SaltedHash();
                string passwordNuevo = password.Text;
                string salt;
                string passCrypted;
                hash.GetHashAndSaltString(passwordNuevo, out passCrypted, out salt);
                cliente.password= hash.GetStringHash(passwordNuevo, salt);
                cliente.salt = salt;
                //cliente.password = passCrypted;
                cliente.Save();
                cliente.DatoTemporal = passwordNuevo;
                cliente.EnviarEmailNuevaCuenta(passwordNuevo);
                Response.Write(WLMFunctions.SerializeObject(
                                new SuccessObject(
                                    SuccessCode.RegistrySaved,
                                    "",
                                    WLMFunctions.SerializeObject(cliente))
                                , true));

            }
            catch (Exception ex)
            {
                Response.Write(WLMFunctions.SerializeObject(
                       new ErrorObject(ErrorCode.CompilationError, ex.Message, ex.Message, ex.StackTrace)
                       , true));
            }
            ConexionDB.CloseConnection();
            Response.End();
        }
    }
}